package com.geeklan.smart.data.repository.device;

import androidx.annotation.NonNull;

import com.geeklan.base.utils.MLog;
import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.Device;
import com.geeklan.smart.data.model.DeviceId;
import com.geeklan.smart.data.model.DeviceList;
import com.geeklan.smart.data.model.DeviceStatusResponse;
import com.geeklan.smart.data.model.DeviceVersion;
import com.geeklan.smart.data.model.Feed;
import com.geeklan.smart.data.model.FeedPlan;
import com.geeklan.smart.data.model.FeedingRecord;
import com.geeklan.smart.data.model.RequestDevice;
import com.geeklan.smart.data.model.RequestDeviceUpdate;
import com.geeklan.smart.data.model.RequestFoodBowl;
import com.geeklan.smart.data.model.TokenResponse;
import com.geeklan.smart.data.model.UploadDeviceVideoImg;
import com.geeklan.smart.data.remote.RetrofitClient;
import com.geeklan.smart.data.remote.device.DeviceApiService;
import com.geeklan.smart.data.repository.RepositoryCallback;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DeviceRepository {
    private final DeviceApiService apiService;

    public DeviceRepository() {
        this.apiService = RetrofitClient.getInstance().getCmdApiService();
    }

    public void getDeviceAccessToken(RequestDevice requestDeviceAccessToken, RepositoryCallback<ApiResponse<TokenResponse>> callback) {
        Call<ApiResponse<TokenResponse>> call = apiService.getDeviceAccessToken(requestDeviceAccessToken);
        call.enqueue(new Callback<ApiResponse<TokenResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<TokenResponse>> call, @NonNull Response<ApiResponse<TokenResponse>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<TokenResponse>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getDeviceById(RequestDevice requestDevice, RepositoryCallback<DeviceStatusResponse> callback) {
        Call<DeviceStatusResponse> call = apiService.getDeviceById(requestDevice);
        call.enqueue(new Callback<DeviceStatusResponse>() {
            @Override
            public void onResponse(@NonNull Call<DeviceStatusResponse> call, @NonNull Response<DeviceStatusResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<DeviceStatusResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getUserDevice(RepositoryCallback<DeviceList> callback) {
        Call<DeviceList> call = apiService.getUserDevice();
        call.enqueue(new Callback<DeviceList>() {
            @Override
            public void onResponse(@NonNull Call<DeviceList> call, @NonNull Response<DeviceList> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception("Failed to fetch user data"));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<DeviceList> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void bindUserDevice(Device device,RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.bindUserDevice(device);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    MLog.i("绑定设备成功" + response.body().getCode());
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void controlOpenFeed(Feed feed, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.controlOpenFeed(feed);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void resetFeedNum(FeedPlan feedPlan, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.resetFeedNum(feedPlan);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void deleteUserBindDevice(Device device, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.deleteUserBindDevice(device);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void updateDeviceInfo(Device device, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.updateDeviceInfo(device);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void clearBowl(Device device, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.clearBowl(device);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void intelligentFoodBowl(RequestFoodBowl requestFoodBowl, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.intelligentFoodBowl(requestFoodBowl);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void firmwareUpgrade(RequestDeviceUpdate requestDeviceUpdate, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.firmwareUpgrade(requestDeviceUpdate);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void uploadDeviceVideoImg(UploadDeviceVideoImg uploadDeviceVideoImg, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.uploadDeviceVideoImg(uploadDeviceVideoImg);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getDeviceVersion(RepositoryCallback<ApiResponse<List<DeviceVersion>>> callback) {
        Call<ApiResponse<List<DeviceVersion>>> call = apiService.getDeviceVersion(100,1,0,0);
        call.enqueue(new Callback<ApiResponse<List<DeviceVersion>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<DeviceVersion>>> call, @NonNull Response<ApiResponse<List<DeviceVersion>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<DeviceVersion>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getFeedingRecordList(String deviceId,String dateDay, RepositoryCallback<ApiResponse<List<FeedingRecord>>> callback) {
        Call<ApiResponse<List<FeedingRecord>>> call = apiService.getFeedingRecordList(deviceId,dateDay);
        call.enqueue(new Callback<ApiResponse<List<FeedingRecord>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<FeedingRecord>>> call, @NonNull Response<ApiResponse<List<FeedingRecord>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<FeedingRecord>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getMonthFeedingRecord(String deviceId,String dateDay, RepositoryCallback<ApiResponse<List<String>>> callback) {
        Call<ApiResponse<List<String>>> call = apiService.getMonthFeedingRecord(deviceId,dateDay);
        call.enqueue(new Callback<ApiResponse<List<String>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<String>>> call, @NonNull Response<ApiResponse<List<String>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<String>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void resetDevice(Device device, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.resetDevice(device);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void sendGetLogCommand(DeviceId deviceId, RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.sendGetLogCommand(deviceId);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }
}
